//logs.js
const app=getApp()
var host = 'https://wuhan2020.m1c.cn';
const util = require('../../utils/util.js')
Page({
  data: {
    logs: [],
    userInfo:""
  },
  onLoad: function () {
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://wuhan2020.m1c.cn/weixin/codeToParam',
            data: {
            code: res.code
            },
            method:'GET',
            header:{'content-type':'applicaton/json'},
            success:function(res){
              app.globalData.openId = res.data.data;
              console.log("获取openid成功:" + app.globalData.openId);
              console.log( app.globalData.userInfo);
            },
            fail:function(error){
              console.log("获取失败"+error.data.error_code)
            }
          })
          console.log("11111111" + app.globalData.openId);
          let openid = app.globalData.openId;
        console.log(openid);
          let name = app.globalData.userInfo.nickName;
          let gender = app.globalData.userInfo.gender;
          let avatarUrl = app.globalData.userInfo.avatarUrl;
          userlogin(openid, name, gender, avatarUrl); //在服务器中注册
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      },
    });
  },
})

//post请求，向后台注册
function userlogin(openid, name, gender, avatarUrl) {
  console.log("请求的openid"+openid);
   wx.request({
     url: 'https://wuhan2020.m1c.cn//user/loginByWx',
     data: {
       openId:openid,
       nickName: name,
       gender: gender,
       avatarUrl: avatarUrl,
     },
     method: 'POST',
     header: { "content-type": "application/json" },
     success: function (res) {
       console.log("后台注册成功");
       console.log(res.data);
     },
   })
 }
 